package server.model.recordModel;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import server.db.DBDriver;
import server.db.DBFactory;
import server.entity.Good;
import server.entity.Record;
import server.util.InfoUtils.Info;
import server.util.InfoUtils.InfoEnum;
import server.util.InfoUtils.InfoUtil;

import java.util.ArrayList;
import java.util.List;

public class RecordModel {
    //添加购买记录
    public Info setRecord(int userId, int goodId, int count) {
        //获取商品具体信息
        String getGoodInfoSql = "select store_id,good_name,price from good where good_id = ?";
        Good good = DBFactory.getDBDriver().query(getGoodInfoSql, new BeanHandler<>(Good.class, DBDriver.getProcess()), goodId);
        if (good == null) {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        }

        String getUserNameSql = "select user_name from user where user_id = " + userId;
        String userName = DBFactory.getDBDriver().queryOne(getUserNameSql);
        if (userName == null) {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        }

        String sql = "insert record (user_id,user_name,good_id,good_name,amount,count,store_id,bought_time) values (?,?,?,?,?,?,?,now()) ";

        //配置record的参数
        ArrayList<Object> params = new ArrayList<>();
        params.add(userId);
        params.add(userName);
        params.add(goodId);
        params.add(good.getGoodName());
        params.add(good.getPrice() * count);
        params.add(count);
        params.add(good.getStoreId());

        int line = DBFactory.getDBDriver().update(sql, params.toArray());
        if (line > 0) {
            return InfoUtil.getInfo(InfoEnum.SUCCESS);
        } else {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        }
    }

    //通过时间查询店铺销售记录
    public Info getRecordByDate(Integer storeId, String startDate, String endDate) {
        String sql = "select * from record where store_id = ? and bought_time >= ? and bought_time <= ?";

        //配置查询的变量
        List<Object> params = new ArrayList<>();
        params.add(storeId);
        params.add(startDate);
        params.add(endDate);

        //查询数据库中的购买记录
        List<Record> records = DBFactory.getDBDriver().query(sql, new BeanListHandler<>(Record.class, DBDriver.getProcess()), params.toArray());
        if (records == null) {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        } else if (records.size() == 0) {
            return InfoUtil.getInfo(1, "该时间段没有记录");
        } else {
            return InfoUtil.getInfo(records);
        }
    }

    //通过时间查询购买记录
    public Info getRecordOfUser(Integer userId) {
        String sql = "select * from record where user_id = ?";

        //查询数据库中的购买记录
        List<Record> records = DBFactory.getDBDriver().query(sql, new BeanListHandler<>(Record.class, DBDriver.getProcess()), userId);
        if (records == null) {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        } else if (records.size() == 0) {
            return InfoUtil.getInfo(1, "没有记录");
        } else {
            return InfoUtil.getInfo(records);
        }
    }
}
